package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.AccountDto;
import com.example.dto.MsgLogDto;
import com.example.dto.ProNormDto;
import com.example.dto.VipDto;
import com.example.exception.MyException;
import com.example.mapper.MsgLogMapper;
import com.example.mapper.VipMapper;
import com.example.service.VipService;
import com.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.Random;

@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, VipDto> implements VipService {
    @Autowired
    private VipMapper mapper;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private MsgLogMapper msgLogMapper;

    @Override
    public void add(VipDto dto) {
        mapper.insert(dto);
    }

    @Override
    public IPage<VipDto> select(VipDto dto) {
        QueryWrapper<VipDto> queryWrapper = new QueryWrapper<>();
        if (dto.getPhone() != null && dto.getPhone() != 0) {
            queryWrapper.eq("phone", dto.getPhone());
        }
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.between("add_time", dto.getStartTime(), dto.getEndTime());
        }
        Page<VipDto> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<VipDto> result = mapper.selectPage(page, queryWrapper);
        return result;
    }

    @Override
    public void update(VipDto dto) {
        QueryWrapper<VipDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dto.getId());
        mapper.update(dto, queryWrapper);
    }

    @Override
    public void delete(VipDto dto) {
        QueryWrapper<VipDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dto.getId());
        mapper.delete(queryWrapper);
    }

    @Override
    public String login(VipDto dto) throws MyException {
        QueryWrapper<VipDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("password", dto.getPassword()).eq("phone", dto.getPhone());
        VipDto vipDto = mapper.selectOne(queryWrapper);
        if (vipDto != null) {
            if (Objects.equals(vipDto.getState(), "0")) {
                throw new MyException("被拉入黑名单，请联系管理员");
            }
            return jwtUtil.createToken(dto.getPhone().toString());
        }
        throw new MyException("账号或密码错误");
    }

    @Override
    public void sendMsg(String phoneCode) throws MyException {
        QueryWrapper<VipDto> qr = new QueryWrapper<>();
        qr.eq("phone", phoneCode);
        Long count = mapper.selectCount(qr);
        if (count == 0) {
            Random random = new Random();
            StringBuffer code = new StringBuffer();
            for (int i = 0; i < 6; i++) {
                code.append(random.nextInt(10));
            }
            msgLogMapper.insert(new MsgLogDto(null, phoneCode, code.toString(), new Date()));
        } else {
            throw new MyException("当前账号已存在");
        }
    }
}
